import java.util.*;
import java.lang.Integer;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class seq {
  public static int longestConsecutive(int[] num) {
    // Start typing your Java solution below
    // DO NOT write main() function
    int seqLen = num.length;
    if (seqLen == 0) {
      return 0;
    }
    if (seqLen == 1) {
      return 1;
    }
    int curMax = 1;
    HashMap<Integer, Integer> start = new HashMap<Integer, Integer>();
    HashMap<Integer, Integer> end = new HashMap<Integer, Integer>();
    int tmpLen = 0;
    for (int i = 0; i < seqLen; i++) {
      if (start.containsKey(num[i]) || end.containsKey(num[i])) {
        continue;
      }
      if (!start.containsKey(num[i] + 1) && !end.containsKey(num[i] - 1)) {
        start.put(num[i], 1);
        end.put(num[i], 1);
      } else {
        if (start.containsKey(num[i] + 1)) {
          tmpLen = start.get(num[i] + 1) + 1;
          start.put(num[i], tmpLen);
          end.put(num[i] + tmpLen - 1, tmpLen);
          if (tmpLen > curMax) {
            curMax = tmpLen;
          }
        } 
        if (end.containsKey(num[i] - 1)) {
          tmpLen = end.get(num[i] - 1) + 1;
          start.put(num[i] - tmpLen + 1, tmpLen);
          end.put(num[i], tmpLen);
          if (tmpLen > curMax) {
            curMax = tmpLen;
          }
        }
        if (start.containsKey(num[i] + 1) && end.containsKey(num[i] - 1)) {
          int leftLen = end.get(num[i]), rightLen = start.get(num[i]);
          tmpLen = leftLen + rightLen - 1;
          start.put(num[i] - leftLen + 1, tmpLen);
          end.put(num[i] + rightLen - 1, tmpLen);
          if (tmpLen > curMax) {
            curMax = tmpLen;
          }
        }
      }
      tmpLen = 0;
      System.out.println("Current round is adding " + num[i] + "; the current max length is " + curMax);
      System.out.println("At round " + i + " Start hashMap: ");
      printHashMap(start);
      System.out.println("At round " + i + " End hashMap: ");
      printHashMap(end);
    }
    return curMax;
  }
  
  public static void printHashMap (HashMap<Integer, Integer> map) {
    if (map == null) {
      return;
    }
    Iterator iterator = map.keySet().iterator();  
    String result = "{";
    while (iterator.hasNext()) {  
       int key = Integer.parseInt(iterator.next().toString());
       int value = map.get(key);  
       result += key + ": " + value + ", ";
    }
    System.out.println(result.substring(0, result.length() - 2) + "}");
  }
  
  public static void main(String[] args) {
    String FILENAME = "D:\\Snoopy\\Programming\\Interview\\Java\\LeetCode\\longest_consecutive_seq\\test.txt";
    Path path = Paths.get(FILENAME);
    try {
      Scanner scanner =  new Scanner(path);
      String nextLine = scanner.next();
      String [] splitedLine = nextLine.substring(1, nextLine.length() - 1).split(",");
      int splitedLen = splitedLine.length;
      int [] numSeq = new int[splitedLen];
      for (int i = 0; i < splitedLen; i++) {
        numSeq[i] = Integer.parseInt(splitedLine[i]);
      }
      System.out.println("Longest sequence length is " + longestConsecutive(numSeq));
      Arrays.sort(numSeq);
      System.out.println("Sorted Array is " + Arrays.toString(numSeq));
      int maxSeq = 1;
      if (splitedLen > 1){
        int tmpLen = 1;
        for (int i = 1; i < splitedLen; i++) {
          if (numSeq[i-1] + 1 == numSeq[i]) {
            tmpLen++;
          } else if (numSeq[i-1] != numSeq[i]) {
            tmpLen = 1;
          }
          if (tmpLen > maxSeq) {
            maxSeq = tmpLen;
          }
        }
      }
      System.out.println("Expected result is " + maxSeq);
    } catch(IOException e) {
      System.out.println("IO Error: file not exist!!!");
    }
    /*  
    Scanner scanner = new Scanner(System.in);
    int totalNum = scanner.nextInt();
    int [] numSeq = new int[totalNum];
    for (int i = 0; i < totalNum; i++) {
      numSeq[i] = scanner.nextInt();
    }
    System.out.println("Longest sequence length is " + longestConsecutive(numSeq));*/
  }
}